home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / BlockBuster / BlockBuster.jar / BlockBuster.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-11-14  |  5.0 KB  |  264 lines

  1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import javax.microedition.lcdui.Command;
  7. import javax.microedition.lcdui.CommandListener;
  8. import javax.microedition.lcdui.Display;
  9. import javax.microedition.lcdui.Displayable;
  10. import javax.microedition.midlet.MIDlet;
  11. import javax.microedition.midlet.MIDletStateChangeException;
  12. import javax.microedition.rms.RecordStore;
  13. import javax.microedition.rms.RecordStoreException;
  14.  
  15. public class BlockBuster extends MIDlet implements CommandListener, Runnable {
  16.    private Command CmdExit = new Command("exit", 1, 2);
  17.    private Command CmdStart = new Command("Start", 1, 3);
  18.    private Command CmdPlay = new Command("play", 1, 3);
  19.    private Command Cmdstop = new Command("stop", 1, 4);
  20.    private Command CmdReStart = new Command("Start", 1, 3);
  21.    private Display display = Display.getDisplay(this);
  22.    private Court court = new Court(this);
  23.    private MsgBox mbox = new MsgBox();
  24.    private boolean paused = true;
  25.    private int StageNo;
  26.    private int gamespeed = 60;
  27.    private RecordStore RStore;
  28.    private Thread gameThread;
  29.    private int highScoreID;
  30.  
  31.    public BlockBuster() {
  32.       this.mbox.addCommand(this.CmdStart);
  33.       this.mbox.addCommand(this.CmdExit);
  34.       this.mbox.setCommandListener(this);
  35.       this.StageNo = 0;
  36.       this.RStore = null;
  37.       this.showTitle();
  38.    }
  39.  
  40.    public void FirstStart() {
  41.       try {
  42.          this.display.setCurrent(this.mbox);
  43.          MsgBox.stgNum = 1;
  44.          MsgBox.msgType = 1;
  45.          this.mbox.repaint();
  46.          Thread.sleep(650L);
  47.          this.mbox.removeCommand(this.CmdStart);
  48.          this.mbox.removeCommand(this.CmdExit);
  49.       } catch (InterruptedException var1) {
  50.       }
  51.  
  52.       this.StageNo = 1;
  53.       this.court.addCommand(this.CmdPlay);
  54.       this.court.addCommand(this.CmdExit);
  55.       this.display.setCurrent(this.court);
  56.       this.court.setCommandListener(this);
  57.    }
  58.  
  59.    public void ShowMsg(int var1) {
  60.       try {
  61.          this.display.setCurrent(this.mbox);
  62.          MsgBox.mscore = Court.score;
  63.          MsgBox.hiscore = Court.hiscore;
  64.          MsgBox.stgNum = this.StageNo;
  65.          MsgBox.msgType = var1;
  66.          this.mbox.repaint();
  67.          if (var1 == 3 || var1 == 5) {
  68.             this.WriteHiScore();
  69.             this.mbox.addCommand(this.CmdReStart);
  70.             this.mbox.addCommand(this.CmdExit);
  71.             this.mbox.setCommandListener(this);
  72.             return;
  73.          }
  74.  
  75.          Thread.sleep(1000L);
  76.          this.display.setCurrent(this.court);
  77.          Court.Msging = false;
  78.       } catch (InterruptedException var2) {
  79.       }
  80.  
  81.    }
  82.  
  83.    public void SlowDown() {
  84.       this.gamespeed += 5;
  85.       if (this.gamespeed > 90) {
  86.          this.gamespeed = 90;
  87.       }
  88.  
  89.    }
  90.  
  91.    public void SpeedUp() {
  92.       this.gamespeed -= 5;
  93.       if (this.gamespeed < 30) {
  94.          this.gamespeed = 30;
  95.       }
  96.  
  97.    }
  98.  
  99.    void ThreadStart() {
  100.       this.gameThread = new Thread(this);
  101.       this.gameThread.start();
  102.    }
  103.  
  104.    public void WriteHiScore() {
  105.       if (this.RStore != null) {
  106.          ByteArrayOutputStream var1 = new ByteArrayOutputStream();
  107.          DataOutputStream var2 = new DataOutputStream(var1);
  108.  
  109.          try {
  110.             var2.writeInt(Court.hiscore);
  111.          } catch (IOException var5) {
  112.          }
  113.  
  114.          byte[] var3 = var1.toByteArray();
  115.  
  116.          try {
  117.             this.RStore.setRecord(this.highScoreID, var3, 0, var3.length);
  118.          } catch (RecordStoreException var4) {
  119.          }
  120.  
  121.       }
  122.    }
  123.  
  124.    public void commandAction(Command var1, Displayable var2) {
  125.       if (var1 == this.CmdExit) {
  126.          this.WriteHiScore();
  127.          this.destroyApp(false);
  128.          ((MIDlet)this).notifyDestroyed();
  129.       } else if (var1 == this.CmdStart) {
  130.          this.ThreadStart();
  131.          this.FirstStart();
  132.          this.court.Playing = true;
  133.          Court.Msging = false;
  134.       } else if (var1 == this.CmdReStart) {
  135.          this.court.ChgMsgStatus(0);
  136.          this.StageNo = 1;
  137.          this.display.setCurrent(this.court);
  138.          Court.score = 0;
  139.          Court.life = 3;
  140.          Court.Msging = false;
  141.          this.court.GoNext(this.StageNo);
  142.          this.court.removeCommand(this.Cmdstop);
  143.          this.court.addCommand(this.CmdPlay);
  144.          this.court.BallMove(false);
  145.          this.mbox.removeCommand(this.CmdReStart);
  146.       } else if (var1 == this.CmdPlay) {
  147.          this.court.Playing = true;
  148.          this.court.BallMove(true);
  149.          this.court.removeCommand(this.CmdPlay);
  150.          this.court.addCommand(this.Cmdstop);
  151.       } else if (var1 == this.Cmdstop) {
  152.          this.court.Playing = false;
  153.          this.court.removeCommand(this.Cmdstop);
  154.          this.court.addCommand(this.CmdPlay);
  155.       }
  156.  
  157.    }
  158.  
  159.    public void destroyApp(boolean var1) {
  160.       this.WriteHiScore();
  161.    }
  162.  
  163.    public void load() {
  164.       Object var1 = null;
  165.       if (this.RStore != null) {
  166.          try {
  167.             if (this.RStore.getNumRecords() == 0) {
  168.                Court.hiscore = 0;
  169.                ByteArrayOutputStream var2 = new ByteArrayOutputStream();
  170.                DataOutputStream var3 = new DataOutputStream(var2);
  171.  
  172.                try {
  173.                   var3.writeInt(Court.hiscore);
  174.                } catch (IOException var5) {
  175.                }
  176.  
  177.                byte[] var4 = var2.toByteArray();
  178.                this.highScoreID = this.RStore.addRecord(var4, 0, var4.length);
  179.             } else {
  180.                this.highScoreID = this.RStore.getNextRecordID();
  181.                if (this.highScoreID > 0) {
  182.                   --this.highScoreID;
  183.                   byte[] var7 = this.RStore.getRecord(this.highScoreID);
  184.                   if (var7 != null) {
  185.                      ByteArrayInputStream var8 = new ByteArrayInputStream(var7);
  186.                      DataInputStream var9 = new DataInputStream(var8);
  187.                      Court.hiscore = var9.readInt();
  188.                   } else {
  189.                      Court.hiscore = 0;
  190.                   }
  191.                }
  192.             }
  193.          } catch (Exception var6) {
  194.          }
  195.  
  196.       }
  197.    }
  198.  
  199.    void pause() {
  200.       this.paused = true;
  201.    }
  202.  
  203.    public void pauseApp() {
  204.       this.pause();
  205.    }
  206.  
  207.    public void run() {
  208.       this.paused = false;
  209.  
  210.       while(!this.paused) {
  211.          this.court.moveBall();
  212.  
  213.          try {
  214.             Thread.sleep((long)this.gamespeed);
  215.          } catch (InterruptedException var1) {
  216.          }
  217.  
  218.          if (this.StageNo == 0) {
  219.             this.FirstStart();
  220.          }
  221.  
  222.          if (this.court.GetMsgStatus() == 2) {
  223.             this.ShowMsg(2);
  224.             ++this.StageNo;
  225.             this.ShowMsg(1);
  226.             this.court.ChgMsgStatus(0);
  227.             this.court.GoNext(this.StageNo);
  228.             this.court.removeCommand(this.Cmdstop);
  229.             this.court.addCommand(this.CmdPlay);
  230.             this.court.BallMove(false);
  231.          } else if (this.court.GetMsgStatus() != 0 && this.court.GetMsgStatus() != 2) {
  232.             this.ShowMsg(this.court.GetMsgStatus());
  233.             this.court.ChgMsgStatus(0);
  234.             this.court.removeCommand(this.Cmdstop);
  235.             this.court.addCommand(this.CmdPlay);
  236.          }
  237.  
  238.          this.court.moveBall();
  239.          this.court.repaint();
  240.       }
  241.  
  242.    }
  243.  
  244.    public void setSpeed(int var1) {
  245.       this.gamespeed = var1;
  246.    }
  247.  
  248.    public void showTitle() {
  249.       this.display.setCurrent(this.mbox);
  250.       MsgBox.msgType = 0;
  251.       this.mbox.repaint();
  252.    }
  253.  
  254.    public void startApp() throws MIDletStateChangeException {
  255.       try {
  256.          this.RStore = RecordStore.openRecordStore("HiScoreDB", true);
  257.       } catch (Exception var1) {
  258.       }
  259.  
  260.       Court.hiscore = 0;
  261.       this.load();
  262.    }
  263. }
  264.